package jointpart.assignment2;

import static org.junit.Assert.*;

import org.junit.Test;

import shared.*;

public class Searcher2Test {

	@Test
	// empty HTMLlist
	public void testGetUrlsA() {
		assertEquals("",Searcher2.getUrls(null, null));
	}
	
	@Test
	// HTMLlist with one url element
	public void testGetUrlsB() {
		HTMLlist testList = new HTMLlist("*PAGE HTTP://1", null);
		assertEquals("",Searcher2.getUrls(testList, "test"));
	}
	
	@Test
	// HTMLlist with url and words
	public void testGetUrlsC() {
		HTMLlist testList = new HTMLlist("*PAGE HTTP://1", null);
		addToHTMLlist (testList,"word");
		assertEquals("",Searcher2.getUrls(testList, "test"));
	}

	@Test
	public void testGetUrlsD() {
		String url1 = "*PAGE HTTP://1";
		HTMLlist testList = new HTMLlist(url1, null);
		addToHTMLlist (testList,"word");
		addToHTMLlist(testList, "anotherword");
		
		assertEquals("HTTP://1\n",Searcher2.getUrls(testList, "word"));
	}
	
	@Test
	// HTMLlist with 2 urls and no match
	public void testGetUrlsE() {
		HTMLlist testList = new HTMLlist("*PAGE HTTP://1", null);
		addToHTMLlist (testList,"word");
		addToHTMLlist(testList, "*PAGE HTTP://2");
		addToHTMLlist(testList, "anotherword");
		assertEquals("",Searcher2.getUrls(testList, "test"));
	}
	
	@Test
	// HTMLlist with 2 urls both with matches
	public void testGetUrlsF() {
		HTMLlist testList = new HTMLlist("*PAGE HTTP1", null);
		addToHTMLlist(testList,"word");
		addToHTMLlist(testList, "*PAGE HTTP2");
		addToHTMLlist(testList, "word");
		assertEquals("HTTP1\nHTTP2\n",Searcher2.getUrls(testList, "word"));
	}
	
	@Test
	// HTMLlist with 2 urls and match in only second
	public void testGetUrlsG() {
		HTMLlist testList = new HTMLlist("*PAGE HTTP1", null);
		addToHTMLlist(testList,"anotherword");
		addToHTMLlist(testList, "*PAGE HTTP2");
		addToHTMLlist(testList, "word");
		assertEquals("HTTP2\n",Searcher2.getUrls(testList, "word"));
	}
	
	/**
	 * Helper function, adds an element to HTMLlist
	 * @param l HTMLlist
	 * @param s String 
	 * @return 
	 */
	private void addToHTMLlist(HTMLlist l, String s) {
		HTMLlist tmp = new HTMLlist (s, null);
		while (!(l.next == null)) {	
			l = l.next;	
		}
		l.next = tmp;
	}
	
}
